package com.laicunba.service.accounting.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.SaleService;
import com.laicunba.service.UserService;
import com.laicunba.service.AccountService.ACCOUNTING_SUBJECT;
import com.laicunba.service.AccountService.RECHARGE_WAY;
import com.laicunba.service.accounting.AccountingException;
import com.laicunba.service.accounting.AccountingTemplate;
import com.laicunba.service.accounting.NegotiateBuyService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.StringUtil;

@Service
public class NegotiateBuyServiceImpl extends AccountingTemplate implements NegotiateBuyService {
	@Autowired
	private SaleService saleService;
	@Autowired
	private UserService userService;
	@Autowired
	private FinancingTargetService financingTargetService;

	@Transactional
	@Override
	public String doBuy(DoBuyParam param) {
		SaleService.MakeFinancingTargetBidParam lockParam = new SaleService.MakeFinancingTargetBidParam();
		lockParam.setCertificateId(param.getCertificateId());
		lockParam.setFinancingTargetId(param.getFinancingTargetId());
		lockParam.setBuyAmount(param.getBuyAmount());
		lockParam.setBuyUserId(param.getBuyUserId());
		AssertUtil.assertTrue(saleService.makeFinancingTargetBid(lockParam), "LOCK_ERROR");

		SaleService.PayedFinancingTargetParam payedParam = new SaleService.PayedFinancingTargetParam();
		payedParam.setCertificateId(param.getCertificateId());
		payedParam.setPayMark(param.getPayMark());
		payedParam.settN(param.gettN());
		AssertUtil.assertTrue(saleService.payedFinancingTarget(payedParam), "PAYED_ERROR");

//		StringBuffer totalSignBuffer = new StringBuffer();
//		// 用户借记
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_BEFORE_ACCOUNTING_ERROR);
//		String sign = makeUserDr(param.getBuyUserId(), ACCOUNTING_SUBJECT.USER_CURRENCY_OFFLINE, param.getBuyAmount(), param.getCertificateId(), "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertNotNull(sign);
//		// 用户贷记
//		sign = makeUserCr(param.getBuyUserId(), ACCOUNTING_SUBJECT.USER_FINANCING_TARGET_BID, param.getBuyAmount(), param.getCertificateId(), "", "");
//		totalSignBuffer.append(sign);
//		// 平台借记
//		sign = makePlatformDr(ACCOUNTING_SUBJECT.CURRENCY_OFFLINE, param.getBuyAmount(), param.getCertificateId(), "", "");
//		totalSignBuffer.append(sign);
//		// 平台贷记
//		sign = makePlatformCr(ACCOUNTING_SUBJECT.FINANCING_TARGET_BID, param.getBuyAmount(), param.getCertificateId(), "", "");
//		totalSignBuffer.append(sign);
//		AssertUtil.assertTrue(theWholeBalanceIsZero(), AccountingException.BALANCE_SHEET_AFTER_ACCOUNTING_ERROR);

//		return StringUtil.md5(totalSignBuffer.toString());
		return "";
	}

}
